<?
define ('PAGE_MAX', 10);

class News_UI_ControlFactory
{
    var $Controller;
    var $groupName;
    var $Find;
    var $Extra;
    function News_UI_ControlFactory($controller)
	{
	    $this->Controller = $controller;
	    $this->groupName  = $this->Controller->Settings['group'];
        $this->Find       = $this->Controller->Settings['find'];
		$this->Extra      = strlen($this->Find) > 0 ? "/find/{$this->Find}" : "";
	}
	
	function Button ($uri, $caption, $icon=NULL, $confirm=NULL)
	{
	    if ($icon)
	    {
	        $tmp  = explode ('/', $icon);
	        $icon = array_pop($tmp);
	    }
	    $ask  = $confirm ? sprintf("onclick='if (!confirm(this.className)) return false;' class='%s'", $confirm) : '';
		$http = array ('ui'      => 'image',
		               'id'      => 'button',
		               'caption' => $caption,
		               'icon'    => $icon
		               );
	    $btn  = $this->Controller->URI ($http);
	    $img  = "<img align='absmiddle' border='0' src='{$btn}'>";
	    return "<a href='{$uri}' {$ask}>{$img}</a>";
	}
	
	function Paginator ($number, $address = NULL, $label = 'page')
	{   
	    $ret  = array(); 
//		$uri  = "/ui/commandhandler/request/page/group/{$this->groupName}{$this->Extra}/page/"; 
		$http = array ('ui'      => 'commandhandler',
		               'request' => 'page',
		               'group'   => $this->groupName,
		               $label    => NULL
		               );
		               
		if ($address) $http = $address; 
		$uri  = $this->Controller->URI ($http) . "/{$label}/";
	    $page = $this->Controller->Settings[$label];
	    if (strlen($page) == 0) $page = 1;
	    $size = ceil($number / UI_PAGE_SIZE);
	    $min  = 1 + (floor (($page - 1) / PAGE_MAX) * PAGE_MAX);
	    $max  = $min + PAGE_MAX; //min  ($min + PAGE_MAX, $size + 1);

	    # page   = 13
	    # number = 246
	    # 1...11 12 13 14 15 16 17 18 19 20...25
	    #      ^ floor (page / PAGE_MAX) * PAGE_MAX
	    #    ^ min - 1
	    #                                 ^ min + PAGE_MAX
	    #                                    ^ max + 1 
	    #                                      ^ ceil (number / PAGE_MAX)
	    
	    if ($page > 1)
	    {
	        $ret[] = sprintf("<a href='%s%s'>%s</a>", $uri, $page - 1, 'Prev');
	    }
	    
	    if ($min > PAGE_MAX)
	    {
	        $ret[] = sprintf("<a href='%s%s'>%s</a>", $uri, 1, 1);    
	        $ret[] = sprintf("<a href='%s%s'>%s</a>", $uri, $min - 1, '...'); 
	    } 
	    for ($x=$min;$x<$max&&$x<$size+1;$x++)
	    {
	        $ret[] = $x==$page ? "<b>{$x}</b>" :  sprintf("<a href='%s%s'>%s</a>", $uri, $x, $x);    
	    }
	    if ($max < $size)
	    {
	        $ret[] = sprintf("<a href='%s%s'>%s</a>", $uri, $max, '...');  
	        $ret[] = sprintf("<a href='%s%s'>%s</a>", $uri, $size, $size);   
	    }   
	
	    if ($page < $size)
	    {
	        $ret[] = sprintf("<a href='%s%s'>%s</a>", $uri, $page + 1, 'Next');
	    }
	    
	    
//	    $ret[] = "{$min},{$max},{$page},{$size},{$number}";
	    return implode (' - ', $ret);
	}
	
	function Thumbnail ($picname, $uri, $caption, $error = '')
	{
	    $blank  = "http://www.101stratford.com/webservices/images/clear.gif";
	    $ret = '';
	    $ret .= "<div class='frame'>";                     
        $ret .= "<table cellspacing='0' cellpadding='4' width='100%' height='200'><tr><td align='center'>";                     
        $ret .= "<a style='font-size:7pt' href='{$uri}'>";                     
        $ret .= strlen($error) > 0 ? $error : "<img onload='preload(this,this.className)' src='{$blank}' class='{$picname}'/>";
        $ret .= "</a></td></tr></table>";
	    $ret .= "<div align='left'>{$caption}</div>";
	    $ret .= "</div>";
        return $ret;     
	}
	
	function TabBar ($name = 'Server')
	{    
	    $link  = array ('Server'=>'/ui/',
	                    'Downloads'=>'/ui/downloads/page/1',
	                    'Bookmarks'=>'/ui/bookmarks/page/1',
	                    'Media'=>'/ui/media/page/1'
	                      );
        if (sizeof ($this->Controller->Queue->Items) > 0)
        {
            $link['Queue'] = '/ui/queue/request/list';
        }
        
        $tabs  = '<ul id="pimtabs">';
        while (list ($text, $uri) = each($link))
        {
            $tabs .= sprintf('<li class="%s"><a href="%s">%s</a></li>', $name==$text?'selected':'', $uri, $text);
        }
       # $tabs .= '<li><a>Disk Usage ' . $this->Controller->Usage . '</a></li>';
        $tabs .= '</ul>';
        return $tabs;
	}
	 
	function GetSpan($filename)
	{
	    $ret = array();
		$xml = simplexml_load_file($filename);
		$total  = $xml->xpath("//article");  
		$least  = $total[0]->id;
		$x      = sizeof($total)-1;
		$more   = '';
		$e      = 0; $n = 0;
		while (strlen($more)==0 && ++$n < 100)
		{
	        $more   = $total[$x--]->id;
		}
		while (strlen($least)==0 && ++$n < 100)
		{
	        $least   = $total[$e++]->id;
		}
		if (strpos($least, '-')!==false) $least = substr($least, 0, strpos($least,'-'));
		if (strpos($more, '-')!==false) $more = substr($more, 0, strpos($more,'-'));
	    return array('lo'=>$least,'hi'=>$more);
	}
	 
	function BatchList()
	{
	    $uri     = "/ui/commandhandler/group/{$this->groupName}/request/restore/more/";
        $oldname = "data/{$this->groupName}.xml";
        $array   = $this->GetSpan($oldname);
	    $drp = "<select class='{$uri}' onchange='location.href=this.className+this.options[this.selectedIndex].value'>";
	    $drp .= sprintf("<option value=''>%s-%s</option>", $array['lo'], $array['hi']);
	    for ($fn=1;$fn<100;$fn++)
	    {
             $newname = "data/{$this->groupName},{$fn}.xml"; 
             if(file_exists($newname))
             {
                 $array   = $this->GetSpan($newname);
                 $drp .= sprintf("<option value='%s'>%s-%s</option>", $fn, $array['lo'], $array['hi']);
             } else break;
	    }
	    $drp .= "</select>";
	    return $drp;
	}
	
	function VideoPlayer ($m4v, $width = 320, $height = 240)
	{
	    $pic = "{$m4v}.jpeg";
	    $player = "";
	    $domain = "http://68.32.79.100";
        $player .= '<table align=center><tr><td>';
        $player .= '<video id="player5" src="/' . $m4v . '" controls="controls"';
        $player .= ' autoplay="autoplay" width="' . $width . '"';
        $player .= ' height="' . $height . '"';
        $player .= ' poster="' . $domain . '/' . $pic . '">';
        $player .= '</video><br/><br/>';//.$m4v; 
        $player .= "</td></tr></table>";
//        $player .= "<img src='/$pic'/>";
        return $player;
	}
	
	function ArticleSearchForm ()
	{
	    $form = "";
        $form .= "<div style='float:right'>";
        $form .= "<form method='post' action='/ui/commandhandler/page/1/request/find/group/{$this->groupName}'>";
        $form .= sprintf("<input type='text' name='find' size='12' value='%s'/>", $this->Controller->Settings['find']);
        $form .= "<input type='submit' value='Find'>";  
		if (strlen($this->Controller->Settings['find']) > 0)
		{
		    $form .= "<a class='linkbutton' href='/ui/articlelist/group/{$this->groupName}/page/1'>Clear</a>"; 
		}
		else 
		{
		}
		    $form .= "<input type='checkbox' name='seek' value='1'/>Full search";	
        $form .= "</form>";
        $form .= "</div>";
        return $form;
	}
	
	function BookmarkSearchForm ()
	{
	    $form = "";
        $form .= "<div style='float:right'>";
        $form .= "<form method='post' action='/ui/bookmarks/page/1'>";
        $form .= sprintf("<input type='text' name='param' size='12' value='%s'/>", $this->Controller->Settings['param']);
        $form .= "<input type='submit' value='Find'>";  
		if (strlen($this->Controller->Settings['param']) > 0)
		{
		    $form .= "<a class='linkbutton' href='/ui/bookmarks/page/1'>Clear</a>"; 
		} 
        $form .= "</form>";
        $form .= "</div>";
        return $form;
	}
}
















